/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "compilationinfodialog.h"

#include <QDialog>
#include <QWidget>

#include <QFrame>

#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QGroupBox>
#include <QPushButton>
#include <QTextEdit>
#include <QScrollArea>

#include <QDebug>

#include "../Types/compilation.h"

#include "utils.h"
#include "../globals.h"

CompilationInfoDialog :: CompilationInfoDialog(const Compilation& c, QWidget* parent/*=0*/)
    :QDialog(parent)
{
    int groupsCounter = 0;
    int artistsCounter = 0;
    int songsCounter = 0;

    this->setWindowTitle(tr("%1 :: NUP Compilation viewer").arg(c.title));
    this->setWindowIcon(QIcon(ICONSDIR + QString("compilation.png")));

    pMainGrid = new QVBoxLayout(this);

    pTitleLabel = new QLabel(QString("<h3><b>") + c.title + QString("</b></h3>"), this);
    if ( !c.label.isEmpty() )
    {
        pLabelLabel = new QLabel(c.label,this);
    }
    else
    {
        pLabelLabel = NULL;
    }

    pHLine1 = new QFrame(this);
    pHLine1->setFrameStyle(QFrame::HLine);
    pHLine1->setLineWidth(1);

    pSourceLabel = new QLabel(tr("Source: ") + QString("<a href=\"") + c.sourcePage.toString() + QString("\">") + c.sourcePage.toString() + QString("</a>"));
    pSourceLabel->setOpenExternalLinks(true);
    if ( !c.purchaseLink.host().isEmpty() )
    {
        pPurchaseLabel = new QLabel(tr("Purchase: ") + QString("<a href=\"") + c.purchaseLink.toString() + QString("\">") + c.purchaseLink.toString() + QString("</a>"));
        pPurchaseLabel->setOpenExternalLinks(true);
    }
    else
    {
        pPurchaseLabel = NULL;
    }

    pDownloadPageLabel = new QLabel(tr("Download: ") + QString("<a href=\"") + c.downloadLink.toString() + QString("\">") + c.downloadLink.toString() + QString("</a>"));
    pDownloadPageLabel->setOpenExternalLinks(true);
    if ( !c.mediaType.isEmpty() )
    {
        pMediaTypeLabel = new QLabel(tr("Media: ") + c.mediaType);
    }
    else
    {
        pMediaTypeLabel = NULL;
    }

    pLengthLabel = new QLabel(tr("Length: ") + c.runningTime.toString());

    pHLine2 = new QFrame(this);
    pHLine2->setFrameStyle(QFrame::HLine);
    pHLine2->setLineWidth(1);

    pDetailsLayout = new QHBoxLayout();
    if ( !c.info.isEmpty() )
    {
        pDescriptionViewer = new QTextEdit(this);
        pDescriptionViewer->setReadOnly(true);
        {
            QString compilationDescription = c.info;
            compilationDescription.prepend(tr("<b><i>Descriptions details:</b></i><br/>"));
            compilationDescription.replace('\n',"<br/>");

            pDescriptionViewer->setHtml(compilationDescription);
        }

        if ( !c.notes.isEmpty() )
        {
            pDescriptionViewer->setMinimumWidth(200);
            //pDescriptionViewer->setMaximumWidth(300);
        }

        pDescriptionViewer->setMinimumHeight(100);
        pDescriptionViewer->setMaximumHeight(150);
    }
    else
    {
        pDescriptionViewer = NULL;
    }

    if ( !c.info.isEmpty() && !c.notes.isEmpty() )
    {
        pVLine1 = new QFrame(this);
        pVLine1->setFrameStyle(QFrame::VLine);
        pVLine1->setLineWidth(1);
    }
    else
    {
        pVLine1 = NULL;
    }

    if ( !c.notes.isEmpty() )
    {
        QString compilationNotes = c.notes;
        compilationNotes.replace('\n',"<br/>");

        pNotesViewer = new QTextEdit(this);
        pNotesViewer->setReadOnly(true);
        pNotesViewer->setHtml(compilationNotes);
        if ( !c.info.isEmpty() )
        {
            pNotesViewer->setMinimumWidth(200);
            //pNotesViewer->setMaximumWidth(300);
        }

        pNotesViewer->setMinimumHeight(100);
        pNotesViewer->setMaximumHeight(150);
    }
    else
    {
        pNotesViewer = NULL;
    }

    pCompilationInfoLayout = new QHBoxLayout();

    if ( c.groups.size() != 0 )
    {
        pGroupsBox = new QGroupBox("Groups",this);
        pGroupsBoxLayout = new QVBoxLayout();
        pGroupsBox->setLayout(pGroupsBoxLayout);
        pGroupsList = new QListWidget(this);
        pGroupsBox->setMaximumWidth(120);
        pGroupsList->setMaximumWidth(100);

        for ( QList<Group>::const_iterator itGroup = c.groups.begin() ; itGroup != c.groups.end() ; itGroup++ , groupsCounter++ )
        {
            pGroupsList->addItem(new QListWidgetItem(itGroup->name));
        }
    }

    pArtistsBox = new QGroupBox(tr("Artists"),this);
    pArtistsBoxLayout = new QVBoxLayout();
    pArtistsBox->setLayout(pArtistsBoxLayout);
    pArtistsList = new QListWidget(this);
    pArtistsBox->setMaximumWidth(220);
    pArtistsList->setMaximumWidth(200);

    for ( QList<Artist>::const_iterator itArtist = c.artists.begin() ; itArtist != c.artists.end() ; itArtist++ , artistsCounter++ )
    {
        QListWidgetItem* pTmpItem = new QListWidgetItem();

        pTmpItem->setText(itArtist->nickname);
        pTmpItem->setData(Qt::DecorationRole,QIcon(Utils::getFlagPathFromName(itArtist->flag)));

        pArtistsList->addItem(pTmpItem);
    }

    pSongsBox = new QGroupBox(tr("Songs"),this);
    pSongsBoxLayout = new QVBoxLayout();
    pSongsBox->setLayout(pSongsBoxLayout);
    pSongsList = new QTableWidget(c.songs.size(),3,this);
    pSongsList->setHorizontalHeaderLabels(QStringList() << tr("Song") << tr("Artists") << tr("Platform"));
    pSongsList->verticalHeader()->hide();

    QHeaderView* pHeaderView = pSongsList->horizontalHeader();
    pHeaderView->setResizeMode(0,QHeaderView::Stretch);
    pHeaderView->setResizeMode(1,QHeaderView::Stretch);
    pHeaderView->setResizeMode(2,QHeaderView::ResizeToContents);

    for ( QList<Song>::const_iterator itSong = c.songs.begin() ; itSong != c.songs.end() ; itSong++ , songsCounter++ )
    {
        pSongsList->setItem(songsCounter,0,new QTableWidgetItem(itSong->title));

        QString artist("");
        for ( QList<Artist>::const_iterator itArtist = itSong->artists.begin() ; itArtist != itSong->artists.end() ; itArtist++ )
        {
            artist += itArtist->nickname + QString(" ");
        }

        pSongsList->setItem(songsCounter,1,new QTableWidgetItem(artist));

        pSongsList->setItem(songsCounter,2,new QTableWidgetItem(itSong->platform));
    }

    pHLine3 = new QFrame(this);
    pHLine3->setFrameStyle(QFrame::HLine);
    pHLine3->setLineWidth(1);

    pUploadInfoLabel = new QLabel(QString(tr("Uploaded the ")) + QString("<b>") + c.uploadDate.toString() + QString("</b>") + QString(tr(" by ")) + QString("<b>") + c.uploader.name + QString("</b>"));

    pValidateButton = new QPushButton(tr("Ok"),this);

    pMainGrid->addWidget(pTitleLabel,0,Qt::AlignHCenter);
    if ( !c.label.isEmpty() )
    {
        pMainGrid->addWidget(pLabelLabel,0,Qt::AlignHCenter);
    }
    pMainGrid->addWidget(pLengthLabel,0,Qt::AlignHCenter);
    pMainGrid->addWidget(pHLine1);
    pMainGrid->addWidget(pSourceLabel);
    if ( !c.purchaseLink.host().isEmpty() )
    {
        pMainGrid->addWidget(pPurchaseLabel);
    }
    pMainGrid->addWidget(pDownloadPageLabel);
    if ( !c.mediaType.isEmpty() )
    {
        pMainGrid->addWidget(pMediaTypeLabel);
    }
    pMainGrid->addWidget(pHLine2);
    pMainGrid->addLayout(pDetailsLayout);
    if ( !c.info.isEmpty() )
    {
        pDetailsLayout->addWidget(pDescriptionViewer);
    }
    if ( !c.info.isEmpty() && !c.notes.isEmpty() )
    {
        pDetailsLayout->addWidget(pVLine1);
    }
    if ( !c.notes.isEmpty() )
    {
        pDetailsLayout->addWidget(pNotesViewer);
    }
    pMainGrid->addLayout(pCompilationInfoLayout);
    if ( c.groups.size() != 0 )
    {
        pCompilationInfoLayout->addWidget(pGroupsBox);
        pGroupsBoxLayout->addWidget(pGroupsList);
    }
    pCompilationInfoLayout->addWidget(pArtistsBox);
    pArtistsBoxLayout->addWidget(pArtistsList);
    pCompilationInfoLayout->addWidget(pSongsBox);
    pSongsBoxLayout->addWidget(pSongsList);
    pMainGrid->addWidget(pHLine3);
    pMainGrid->addWidget(pUploadInfoLabel,0,Qt::AlignHCenter);
    pMainGrid->addWidget(pValidateButton);

    connect(pValidateButton,SIGNAL(clicked()),this,SLOT(accept()));

    qDebug() << "CompilationInfoDialog created";
}

CompilationInfoDialog :: ~CompilationInfoDialog(void)
{
    qDebug() << "CompilationInfoDialog deleted";
}

void CompilationInfoDialog :: accept(void)
{
    qDebug() << "CompilationInfoDialog :: accept";

    this->done(QDialog::Accepted);
    emit deleteCIDSignal(this);
}

void CompilationInfoDialog :: reject(void)
{
    qDebug() << "CompilationInfoDialog :: reject";

    this->done(QDialog::Rejected);
    emit deleteCIDSignal(this);
}
